<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Moneystage_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_user_stage_by_id($id){

        $this->db->select('*');
        $this->db->from('user_stage');
        $this->db->where('id',$id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改分期信息
     */
    public function sava_user_stage($id,$data){

        $this->db->where('id',$id);
        $this->db->update('user_stage', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增分期
     */
    public function add_user_stage($data){

        $this->db->insert('user_stage',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }
    public function add_sale_satge($data){

        $this->db->insert('money_sale_stage',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }
    /**
     * @return mixed
     * 查询所有分期
     */
    public function get_all_user_stage($start,$limit,$user_id=""){
        $this->db->select('*');
        $this->db->from('user_stage');
        if(!empty($user_id)){
            $this->db->where('user_id',$user_id);
        }
        $this->db->limit($limit,$start)->order_by('user_id','DESC');
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_user_stage_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}user_stage  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除分期
     */
    public function delete_user_stage_by($where){
        $this->db->where($where)->delete('user_stage');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


    public function count_goods_list_by_catagory_id($category_tid){
        $this->db->select('id');
        $this->db->from('user_stage');
        $this->db->where('good_category',$category_tid);
        $this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    public function get_link_by_id($id){
        $this->db->select('*');
        $this->db->from('money_sale_stage');
        $this->db->where('id',$id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_list_stage_by_fenqi_arr($fenqi_arr){
        $this->db->select('*');
        $this->db->from('user_stage');
        $this->db->order_by('stage_num','ASC');
        $this->db->where_in('id',$fenqi_arr);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_all_user_stage_list($start,$limit){
        $this->db->select('*');
        $this->db->from('user_stage');
        $this->db->limit($limit,$start)->order_by('user_id','DESC');
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
}
